package com.example.day02.fragment;


import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.day02.R;
import com.example.day02.adapter.FuLiAdapter;
import com.example.day02.base.BaseFragment;
import com.example.day02.bean.FuLiBean;
import com.example.day02.presenter.FuLiPresenter;
import com.example.day02.view.FuLIView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements FuLIView {
    @BindView(R.id.home_re)
    RecyclerView home_re;
    private ArrayList<FuLiBean.ResultsBean> resultsBeans;
    private FuLiAdapter fuLiAdapter;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        fuLiAdapter.setOnContextClickListener(new FuLiAdapter.OnContextClickListener() {
            @Override
            public void oncontextcliklistener(int pos) {
                View inflate = LayoutInflater.from(getContext()).inflate(R.layout.pop, null);
                PopupWindow popupWindow = new PopupWindow(inflate, 1000, 1000);
                TextView text = inflate.findViewById(R.id.pop_text);
                Button bu = inflate.findViewById(R.id.pop_bu);
                Button bu1 = inflate.findViewById(R.id.pop_bu1);
                text.setText(resultsBeans.get(pos).get_id());
                popupWindow.showAsDropDown(home_re,-1000,-1500);
                bu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
                bu1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        initnotification();
                        popupWindow.dismiss();
                    }
                });
            }
        });
    }
    @SuppressLint("ServiceCast")
    private void initnotification() {
        NotificationManager mager = (NotificationManager) getContext().getSystemService(Context.NETWORK_STATS_SERVICE);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel notificationChannel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            mager.createNotificationChannel(notificationChannel);
        }
        Notification asdadssa = new NotificationCompat.Builder(getContext(), "1")
                .setContentText("trdfugj")
                .setContentTitle("asdadssa")
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setNumber(3)
                .build();
        mager.notify(3,asdadssa);
    }
    @Override
    protected void initView() {
        home_re.setLayoutManager(new LinearLayoutManager(getContext()));
        home_re.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        resultsBeans = new ArrayList<>();
        fuLiAdapter = new FuLiAdapter(getContext(), resultsBeans);
        home_re.setAdapter(fuLiAdapter);
    }

    @Override
    protected void initPresenter() {
        mPresenter=new FuLiPresenter();
        ((FuLiPresenter) mPresenter).getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void onSuccess(FuLiBean data) {
        resultsBeans.addAll(data.getResults());
        fuLiAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {
        Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(getContext(),str,Toast.LENGTH_LONG).show();
    }
}
